package com.ms.pda.components

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.TextView
import com.blankj.utilcode.util.StringUtils
import com.lxj.xpopup.core.CenterPopupView
import com.ms.pda.R
import com.ms.pda.interf.OnDialogCallback

@SuppressLint("ViewConstructor")
class ConfirmDialog(
    context: Context,
    content: String?,
    callback: OnDialogCallback?,
    hideCancel: Boolean
) : CenterPopupView(context) {

    private var mContentTv: TextView? = null
    private var mContent: String?
    private var mCallback: OnDialogCallback? = null
    private var mHideCancel: Boolean = false
    private var mConfirmBtn: String? = null

    init {
        mContent = content
        mCallback = callback
        mHideCancel = hideCancel
    }

    constructor(
        context: Context,
        content: String?,
        callback: OnDialogCallback?,
        hideCancel: Boolean,
        confirmBtn: String?
    ) : this(
        context,
        content,
        callback,
        hideCancel
    ) {
        mConfirmBtn = confirmBtn
    }

    override fun getImplLayoutId(): Int {
        return R.layout.dialog_confirm
    }

    override fun onCreate() {
        super.onCreate()
        mContentTv = findViewById(R.id.dialog_content)
        val confirmBtn: TextView = findViewById<View>(R.id.dialog_confirm) as TextView
        confirmBtn.setOnClickListener {
            smartDismiss()
            mCallback?.onConfirm()
        }
        mConfirmBtn?.let {
            confirmBtn.text = it
        }
        if (mHideCancel) {
            findViewById<View>(R.id.dialog_cancel).visibility = GONE
            findViewById<View>(R.id.dialog_divider).visibility = GONE
        } else {
            findViewById<View>(R.id.dialog_cancel).setOnClickListener {
                smartDismiss()
                mCallback?.onCancel()
            }
        }
        if (!StringUtils.isTrimEmpty(mContent)) mContentTv?.text = mContent
    }

}